//
//  DXHomeOriginalView.swift
//  新浪微博
//
//  Created by dix on 2017/2/6.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit

class DXHomeOriginalView: UIView {
    //记录原创微博的底部约束
    var orginalViewBotttomConstraint: Constraint?
    //提供属性供外界赋值
    var statusViewModel: DXStatusViewModel?{
        didSet{
            //赋值
            //头像
            headImageView.dx_setImage(urlStr: statusViewModel?.homeModel?.user?.profile_image_url)
            //昵称
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            //内容
            contentLabel.text = statusViewModel?.homeModel?.text
            //会员等级
            membershipImageView.image = statusViewModel?.membershipImage
            //会员认证
            avatarImageView.image = statusViewModel?.avatarImage
            orginalViewBotttomConstraint?.uninstall()
            //判断是否有配图
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0  {
                
                self.snp_makeConstraints(closure: { (make) in
                   orginalViewBotttomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.pic_urls = picUrls
                //显示
                pictureView.isHidden = false
            }else {
                //没有配图
                self.snp_makeConstraints(closure: { (make) in
                   orginalViewBotttomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏
                pictureView.isHidden = true
            }
            //设置测试数据
            timeLabel.text = "黑马23时间"
            sourceLabel.text = "来自黑马23"
        }
    }    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        //设置随机颜色
        backgroundColor = UIColor.white
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        headImageView.snp_makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headImageView.snp_right).offset(10)
            make.top.equalTo(headImageView)
        }
        membershipImageView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(10)
            make.top.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.width.equalTo(DXSCREENW - 20)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            //方式2
//            make.bottom.equalTo(self).offset(-10)
        }
        //配图约束
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        snp_makeConstraints { (make) in
           orginalViewBotttomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        //当前原创微博的底部(在view中可以在cell中这么约束不可以)
        //方式1
//        snp_makeConstraints { (make) in
//            make.bottom.equalTo(contentLabel).offset(10)
//        }
        
    }
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    //昵称
    private lazy var nameLabel: UILabel = UILabel(fontSize: DXNORMALFONTSIZE, textColor: UIColor.black)
    //会员等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: DXSMALLFONTSIZE, textColor: DXThemeColor)
    //来自
    private lazy var sourceLabel: UILabel = UILabel(fontSize: DXSMALLFONTSIZE, textColor: DXThemeColor)
    //微博认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel: UILabel = {
        let lab = UILabel(fontSize: DXNORMALFONTSIZE, textColor: UIColor.black)
        //换行
        lab.numberOfLines = 0
        return lab
    
    }()
    private lazy var pictureView:DXHomePictureView = {
        let view = DXHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
